package com.example.mychess;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;

import java.sql.Date;
import java.sql.SQLException;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.example.mychess.Sever.root;


public class UserSever {
    public Text number;
    List<User> list;
    public TableView<User> selectTableView;
    public TableColumn<User, String> account;
    public TableColumn<User, String> password;
    public TableColumn<User, String> name;
    public TableColumn<User, String> sex;
    public TableColumn<User, String> psst;
    public TableColumn<User, Integer> integral;
    public TextField Account;
    public TextField Password;
    public TextField Name;
    public TextField From;
    public TextField End;
    public ComboBox Sex;
    public ComboBox Psst;

    public void select(ActionEvent actionEvent) throws SQLException {
        list = root.find(Account.getText(), Password.getText(), Name.getText(), getSex(), getPsst()
                , Objects.equals(From.getText(), "")||From.getText()==null ?  null:Integer.valueOf(From.getText()) ,
                Objects.equals(End.getText(), "")||End.getText()==null ? null:Integer.valueOf(End.getText()));
        updateTableView(list);
        selectTableView.refresh();
        number.setText(String.valueOf(list.size()));
    }

    public String getSex() {
        if (Sex.getValue() == "" || Sex.getValue() == null) {
            return null;
        } else if (Sex.getValue().equals("男")) {
            return "男";
        }
        return "女";
    }

    public String getPsst() {
        if (Psst.getValue() == "" || Psst.getValue() == null) {
            return null;
        } else if (Psst.getValue().equals("在线")) {
            return "在线";
        }
        return "不在线";
    }

    @FXML
    private void initialize() throws SQLException {
        list = new ArrayList<>();
// 使用学生数据更新TableView
    }

    private void updateTableView(List<User> students) {
// 传入List集合，更新TableView
        selectTableView.getItems().setAll(list);
// 为每列设置单元格值工厂，以从学生对象中获取属性值显示。
        account.setCellValueFactory(new PropertyValueFactory<>("account"));
        password.setCellValueFactory(new PropertyValueFactory<>("password"));
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
        psst.setCellValueFactory(new PropertyValueFactory<>("psst"));
        integral.setCellValueFactory(new PropertyValueFactory<>("integral"));
        // 使用Lambda表达式为表格列设置自定义的单元格工厂
    }

    public void IN(ActionEvent actionEvent) {
        list=EXCEL.readListObject("C:\\Users\\86173\\Desktop\\JAVACode\\MyChess\\user.xlsx",User.class);
        updateTableView(list);
    }

    public void PUT(ActionEvent actionEvent) {
        EXCEL.writeListObject(list,"C:\\Users\\86173\\Desktop\\JAVACode\\MyChess\\user.xlsx",User.class);
    }
}

